{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from __future__ import print_function\n",
    "import re\n",
    "import sys\n",
    "import cPickle as pickle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "datasetDirectory = './Wikipedia Abstracts/Data/'\n",
    "hashDirectory = './Wikipedia Abstracts/hashTable.p'\n",
    "def generateCheckpoint(hashedDataset, dataset, fileNumber, end):\n",
    "    print('\\r{} items have been processed... '.format((fileNumber - 1) * 500000 + len(dataset)), end='')\n",
    "    if end is False and len(dataset) % 500000 == 0 and len(dataset) > 0:\n",
    "        fileLocation = datasetDirectory + ('%d.p' % (fileNumber))\n",
    "        # Comment-out for better performance.\n",
    "        # pickle.dump(hashedDataset, open( hashDirectory, \"wb\" ) )\n",
    "        pickle.dump(dataset, open(fileLocation, \"wb\" ) )\n",
    "        print('Checkpoint file has been successfully stored at: %s...' % (fileLocation))\n",
    "        del dataset [:]\n",
    "        fileNumber += 1\n",
    "    elif end is True:\n",
    "        fileLocation = datasetDirectory + ('%d.p' % (fileNumber))\n",
    "        pickle.dump(hashedDataset, open( hashDirectory, \"wb\" ) )\n",
    "        pickle.dump(dataset, open(fileLocation, \"wb\" ) )\n",
    "        print('Last checkpoint file has been successfully stored at: %s...' % (fileLocation))\n",
    "        del dataset [:]\n",
    "    return fileNumber"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  2016-11-05T13:28:42Z\n",
      "36839 items have been processed... "
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-3-dd9ae123caf5>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m     20\u001b[0m             \u001b[0mtempPageTitle\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mre\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfindall\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mr\"(?<=en\\.wikipedia\\.org\\/wiki\\/)(.*)(?=\\?oldid=)\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtempUrl\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     21\u001b[0m             \u001b[0;32mif\u001b[0m \u001b[0mlinePageTitle\u001b[0m \u001b[0;34m!=\u001b[0m \u001b[0mtempPageTitle\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 22\u001b[0;31m                 \u001b[0mfileNumber\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mgenerateCheckpoint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mitem2pos\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mitems\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfileNumber\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mFalse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     23\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     24\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m<ipython-input-2-a9f326aa975d>\u001b[0m in \u001b[0;36mgenerateCheckpoint\u001b[0;34m(hashedDataset, dataset, fileNumber, end)\u001b[0m\n\u001b[1;32m      2\u001b[0m \u001b[0mhashDirectory\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'./Wikipedia Abstracts/hashTable.p'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      3\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mgenerateCheckpoint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mhashedDataset\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdataset\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfileNumber\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mend\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m     \u001b[0;32mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'\\r{} items have been processed... '\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfileNumber\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0;36m500000\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdataset\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mend\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m''\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      5\u001b[0m     \u001b[0;32mif\u001b[0m \u001b[0mend\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0mFalse\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdataset\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m%\u001b[0m \u001b[0;36m500000\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m0\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdataset\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      6\u001b[0m         \u001b[0mfileLocation\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdatasetDirectory\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m'%d.p'\u001b[0m \u001b[0;34m%\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mfileNumber\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/usr/local/lib/python2.7/site-packages/ipykernel/iostream.pyc\u001b[0m in \u001b[0;36mwrite\u001b[0;34m(self, string)\u001b[0m\n\u001b[1;32m    323\u001b[0m                     \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mflush\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    324\u001b[0m             \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 325\u001b[0;31m                 \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_schedule_flush\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    326\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    327\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0mwritelines\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msequence\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/usr/local/lib/python2.7/site-packages/ipykernel/iostream.pyc\u001b[0m in \u001b[0;36m_schedule_flush\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m    243\u001b[0m         \u001b[0mcall\u001b[0m \u001b[0mthis\u001b[0m \u001b[0mon\u001b[0m \u001b[0mwrite\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mto\u001b[0m \u001b[0mindicate\u001b[0m \u001b[0mthat\u001b[0m \u001b[0mflush\u001b[0m \u001b[0mshould\u001b[0m \u001b[0mbe\u001b[0m \u001b[0mcalled\u001b[0m \u001b[0msoon\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    244\u001b[0m         \"\"\"\n\u001b[0;32m--> 245\u001b[0;31m         \u001b[0;32mwith\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_flush_lock\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    246\u001b[0m             \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_flush_timeout\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    247\u001b[0m                 \u001b[0;32mreturn\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "abstractsDirectory = './Wikipedia Abstracts/long_abstracts_en.tql' \n",
    "items = []\n",
    "item2pos = {}\n",
    "fileNumber = 1\n",
    "with open(abstractsDirectory) as abstractsFile:\n",
    "    linePageTitle = ''\n",
    "    for line in abstractsFile:\n",
    "        line = line.split()\n",
    "        tempSubject = line[0]\n",
    "        tempUrl = line[-2]\n",
    "        \n",
    "        line[0] = ''\n",
    "        line[-2] = ''\n",
    "        line = ' '.join(line)\n",
    "        tempObject = re.findall(r\"(?<=\\\")(.*)(?=\\\"@en)\", line)\n",
    "        if len(tempObject) == 0: tempObject = re.findall(r\"(?<=\\\")(.*)(?=\\\"\\^\\^)\", line)\n",
    "\n",
    "\n",
    "        if len(tempObject) > 0:\n",
    "            tempPageTitle = re.findall(r\"(?<=en\\.wikipedia\\.org\\/wiki\\/)(.*)(?=\\?oldid=)\", tempUrl)[0]\n",
    "            if linePageTitle != tempPageTitle:\n",
    "                fileNumber = generateCheckpoint(item2pos, items, fileNumber, False)\n",
    "                \n",
    "\n",
    "                items.append({'formalPageTitle': tempPageTitle})\n",
    "                items[len(items) - 1]['summary'] = tempObject[0]\n",
    "                items[len(items) - 1]['url'] = re.sub(r'\\?oldid=.*$', '', tempUrl[1:-1])\n",
    "                item2pos[tempPageTitle] = [('%d.p' % (fileNumber)), len(items) - 1]\n",
    "                linePageTitle = tempPageTitle\n",
    "        else:\n",
    "            print (line)\n",
    "    generateCheckpoint(item2pos, items, fileNumber, True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
